package com.ruoyi.cderp.service.impl;

import java.util.List;

import com.ruoyi.cderp.domain.CustomerSettleFeeSheet;
import com.ruoyi.cderp.domain.CustomerSettleFeeSheetDetail;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.CustomerSettlePreSheetDetail;
import com.ruoyi.cderp.mapper.CustomerSettlePreSheetMapper;
import com.ruoyi.cderp.domain.CustomerSettlePreSheet;
import com.ruoyi.cderp.service.ICustomerSettlePreSheetService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 客户预付款单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-11
 */
@Service
public class CustomerSettlePreSheetServiceImpl implements ICustomerSettlePreSheetService
{
    @Autowired
    private CustomerSettlePreSheetMapper customerSettlePreSheetMapper;

    /**
     * 查询客户预付款单
     *
     * @param id 客户预付款单主键
     * @return 客户预付款单
     */
    @Override
    public CustomerSettlePreSheet selectCustomerSettlePreSheetById(String id)
    {
        CustomerSettlePreSheet customerSettlePreSheet = customerSettlePreSheetMapper.selectCustomerSettlePreSheetById(id);
        List<CustomerSettlePreSheetDetail>  list = customerSettlePreSheetMapper.selectPreSheetDetail(id);
        customerSettlePreSheet.setCustomerSettlePreSheetDetailList(list);
        return customerSettlePreSheet;
    }

    /**
     * 查询客户预付款单列表
     *
     * @param customerSettlePreSheet 客户预付款单
     * @return 客户预付款单
     */
    @Override
    public List<CustomerSettlePreSheet> selectCustomerSettlePreSheetList(CustomerSettlePreSheet customerSettlePreSheet)
    {
        return customerSettlePreSheetMapper.selectCustomerSettlePreSheetList(customerSettlePreSheet);
    }

    /**
     * 新增客户预付款单
     *
     * @param customerSettlePreSheet 客户预付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCustomerSettlePreSheet(CustomerSettlePreSheet customerSettlePreSheet)
    {
        customerSettlePreSheet.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人编号
        customerSettlePreSheet.setCreateById(loginUser.getUser().getUserId().toString());
        //创建人名称
        customerSettlePreSheet.setCreateBy(loginUser.getUser().getUserName());

        int rows = customerSettlePreSheetMapper.insertCustomerSettlePreSheet(customerSettlePreSheet);
        insertCustomerSettlePreSheetDetail(customerSettlePreSheet);
        return rows;
    }

    /**
     * 修改客户预付款单
     *
     * @param customerSettlePreSheet 客户预付款单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCustomerSettlePreSheet(CustomerSettlePreSheet customerSettlePreSheet)
    {
        customerSettlePreSheet.setUpdateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人编号
        customerSettlePreSheet.setUpdateById(loginUser.getUser().getUserId().toString());
        //修改人名称
        customerSettlePreSheet.setUpdateBy(loginUser.getUser().getUserName());

        customerSettlePreSheetMapper.deleteCustomerSettlePreSheetDetailBySheetId(customerSettlePreSheet.getId());
        insertCustomerSettlePreSheetDetail(customerSettlePreSheet);
        return customerSettlePreSheetMapper.updateCustomerSettlePreSheet(customerSettlePreSheet);
    }

    /**
     * 批量删除客户预付款单
     *
     * @param ids 需要删除的客户预付款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettlePreSheetByIds(String[] ids)
    {
        customerSettlePreSheetMapper.deleteCustomerSettlePreSheetDetailBySheetIds(ids);
        return customerSettlePreSheetMapper.deleteCustomerSettlePreSheetByIds(ids);
    }

    /**
     * 删除客户预付款单信息
     *
     * @param id 客户预付款单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCustomerSettlePreSheetById(String id)
    {
        customerSettlePreSheetMapper.deleteCustomerSettlePreSheetDetailBySheetId(id);
        return customerSettlePreSheetMapper.deleteCustomerSettlePreSheetById(id);
    }

    /**
     * 新增客户预付款单明细信息
     *
     * @param customerSettlePreSheet 客户预付款单对象
     */
    public void insertCustomerSettlePreSheetDetail(CustomerSettlePreSheet customerSettlePreSheet)
    {
        List<CustomerSettlePreSheetDetail> customerSettlePreSheetDetailList = customerSettlePreSheet.getCustomerSettlePreSheetDetailList();
        String id = customerSettlePreSheet.getId();
        if (StringUtils.isNotNull(customerSettlePreSheetDetailList))
        {
            List<CustomerSettlePreSheetDetail> list = new ArrayList<CustomerSettlePreSheetDetail>();
            for (CustomerSettlePreSheetDetail customerSettlePreSheetDetail : customerSettlePreSheetDetailList)
            {
                customerSettlePreSheetDetail.setSheetId(id);
                list.add(customerSettlePreSheetDetail);
            }
            if (list.size() > 0)
            {
                customerSettlePreSheetMapper.batchCustomerSettlePreSheetDetail(list);
            }
        }
    }
}
